extends Node

var utils = null
var ctrl = null
var base = null
var path = null
var window
var nowround:int = 0
var sell1 = null
var sell2 = null
var list = ["jt","mfq","sp","txz","ypz","sf"]
func _ready():
	pass
func _init():
	call_deferred("init")
func _connect():
    pass
#初始化
func init():
	if globalData.infoDs.has("g_AneHekBase"):
		base = globalData.infoDs["g_AneHekBase"]
		path = chaData.infoDs["cTDW"].dir
	if base != null:
		#初始化
		utils = globalData.infoDs["g_AneHekUtils"]
		ctrl = globalData.infoDs["g_AneHek"]
		base.connect("onSaveGame",self,"resetData")
		base.connect("onStartGame", self, "createWin")
		base.connect("onNewGame",self,"resetdata2")
#清除数据
func resetData():
	if is_instance_valid(window):
		#context.queue_free()
		window.queue_free()
		#context = null
		window = null
#重置数据
func resetdata2():
	sell1 = null
	sell2 = null
	freshsell()
#创建窗口
func createWin():
	nowround = 0
	if is_instance_valid(window):return
	sys.main.connect("onBattleStart",self,"getround")
	window = utils.cWin(sys.main.get_node("ui"),"材料出售",Vector2(800,300))
	for i in range(6):
		var itemBt = load("res://ui/item/item.tscn").instance()
		var tit = sys.newItem("i_Hide_rw_"+list[i])
		itemBt.init(tit)
		itemBt.isDrag = false
		window.add_child(itemBt)
		itemBt.set_position(Vector2(100 + i * 110,50))
		var val = 0
		val = utils[list[i]]
		var lb = utils.cLb(str(val),window)
		lb.set_position(Vector2(116 + i * 110,100))
	var glist = GridContainer.new()
	glist.set_columns(6)
	glist.rect_position = Vector2(80,150)
	window.add_child(glist)
	utils.cBtns(glist,self," 出售1个 ","sellit",[list[0],1])
	utils.cBtns(glist,self," 出售1个 ","sellit",[list[1],1])
	utils.cBtns(glist,self," 出售1个 ","sellit",[list[2],1])
	utils.cBtns(glist,self," 出售1个 ","sellit",[list[3],1])
	utils.cBtns(glist,self," 出售1个 ","sellit",[list[4],1])
	utils.cBtns(glist,self," 出售1个 ","sellit",[list[5],1])
	utils.cBtns(glist,self," 出售5个 ","sellit",[list[0],5])
	utils.cBtns(glist,self," 出售5个 ","sellit",[list[1],5])
	utils.cBtns(glist,self," 出售5个 ","sellit",[list[2],5])
	utils.cBtns(glist,self," 出售5个 ","sellit",[list[3],5])
	utils.cBtns(glist,self," 出售5个 ","sellit",[list[4],5])
	utils.cBtns(glist,self," 出售5个 ","sellit",[list[5],5])
#出售材料
func sellit(name,cnt):
	if utils[name] < cnt:
		sys.newBaseMsg("警告","材料不足！出售失败！")
		return
	utils[name] -= cnt
	sys.main.player.plusGold(cnt * 12)
	sys.newBaseMsg("出售成功","获得金币:"+str(cnt * 12))
	freshlabel()
#刷新材料数量文本
func freshlabel():
	var tmp = 0
	for i in window.get_children():
		if i is Label:
			i.text = str(utils[list[tmp]])
			tmp += 1
#窗口弹出
func showit():
	freshlabel()
	window.popup_centered()
#计算战斗场次
func getround():
	nowround += 1
	if sys.test:
		freshsell()
	if nowround == 10:
		nowround = 0
		freshsell()

func freshsell(): 				
	var rnum = sys.rndRan(1,10000)
	if rnum <= 1000:
		sell1 = sys.newItem(utils.getRndItem("RARE"))
	elif rnum <= 2000:
		sell1 = sys.newItem(utils.getRndItem("NORMAL"))
	elif rnum <= 5000:
		sell1 = sys.newItem(utils.getRndItem("MYALL"))
	else:
		sell1 = sys.newItem(utils.getRndItem())
	sell2 = sys.newItem(utils.getRndMaterial())
	if sys.rndPer(50):
		sell2 = sys.newItem(utils.getRndMaterial2())

func freshsellshow(win):
	if sys.test:
		sys.main.player.plusGold(10000)
	for i in win.get_children():
		if i is Button:
			if i.text =="购买装备":
				if !sell1:
					i.disabled = true
					continue
				i.disabled = false
				if i.is_connected("pressed",self,"sellnow"):
					i.disconnect("pressed",self,"sellnow")
					i.connect("pressed",self,"sellnow",[sell1,win])
			if i.text == "购买材料":
				if !sell2:
					i.disabled = true
					continue
				i.disabled = false
				if i.is_connected("pressed",self,"sellnow"):
					i.disconnect("pressed",self,"sellnow")
					i.connect("pressed",self,"sellnow",[sell2,win])
	if sell1:
		var sell1ico = load("res://ui/item/item.tscn").instance()
		sell1ico.init(sell1)
		sell1ico.isDrag = false
		win.add_child(sell1ico)
		sell1ico.rect_position = Vector2(95,35)

	if sell2:
		var sell2ico = load("res://ui/item/item.tscn").instance()
		sell2ico.init(sell2)
		sell2ico.isDrag = false
		win.add_child(sell2ico)
		sell2ico.rect_position = Vector2(95,145)

func sellnow(item,win):
	var needgold = 0
	if item.id.find("i_Hide") != -1:
		if item.id.find("_rw") != -1:
			needgold = 50
		else:
			needgold = 67
	else:
		needgold = 250
	if sys.main.player.gold >= needgold:
		sys.main.player.plusGold(-needgold)
		if sell1 == item:
			sell1 = null
		else:
			sell2 = null
		freshsellshow(win)
		sys.newBaseMsg("提示","购买成功！")
		if item.id.find("_rw") == -1:
			sys.main.player.addItem(item)
		else:
			utils[item.id.replace("i_Hide_rw_","")] += 1
	else:
		sys.newBaseMsg("提示","金币不足{0}/{1}".format({"0":int(sys.main.player.gold),"1":int(needgold)}))
	pass